package cn.edu.sjtu.se.dslab.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.sjtu.se.dslab.component.data.DataType;
import cn.edu.sjtu.se.dslab.component.data.DomainEntity;
import cn.edu.sjtu.se.dslab.component.data.EquipmentCategory;
import cn.edu.sjtu.se.dslab.component.data.EquipmentProperty;
import cn.edu.sjtu.se.dslab.service.IDataTypeService;
import cn.edu.sjtu.se.dslab.service.IDomainEntityService;
import cn.edu.sjtu.se.dslab.service.IEquipmentCategoryService;
import cn.edu.sjtu.se.dslab.service.IEquipmentPropertyService;

@Controller
@RequestMapping(value = "/domain_entity/equipment/category")
public class EquipmentCategoryController {
	@Resource(name = "dataTypeService")
	IDataTypeService dataTypeService;
	
	@Resource(name="equipmentCategoryService")
	IEquipmentCategoryService equipmentCategoryService;
	
	@Resource(name="equipmentPropertyService")
	IEquipmentPropertyService equipmentPropertyService;
	
	@Resource(name = "domainEntityService")
	IDomainEntityService domainEntityService;
	
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addEquipmentCategory() {
		ModelAndView model = new ModelAndView();
		model.setViewName("add_equipment_category");
		try {
			List<DataType> _typeList = dataTypeService.getDataTypeList();
			List<DomainEntity> typeList = domainEntityService.getAllDomainEntity();
			typeList.addAll(_typeList);
			model.addObject("typeList", typeList);
			return model;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return model;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView getEquipmentCategory() {
		ModelAndView model = new ModelAndView();
		model.setViewName("equipment_category");
		try {
			List<EquipmentCategory> categoryList = equipmentCategoryService.getAllEquipmentCategoryList();
			model.addObject("list", categoryList);
			return model;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return model;
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ModelAndView getEquipmentCategory(@PathVariable(value = "id") String id) {
		ModelAndView model = new ModelAndView();
		model.setViewName("equipment_category_detail");
		try {
			EquipmentCategory  category = equipmentCategoryService.getEquipmentCategoryById(Long.parseLong(id));
			model.addObject("category", category);
			return model;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			List<EquipmentCategory> categoryList = equipmentCategoryService.getAllEquipmentCategoryList();
			model.addObject("list", categoryList);
		}
		return model;
	}
	
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String deleteEquipmentCategory(@PathVariable(value = "id") String id) {
		try {
			EquipmentCategory  category = equipmentCategoryService.getEquipmentCategoryById(Long.parseLong(id));
			equipmentCategoryService.deleteEquipmentCategory(category);
			return "redirect:/domain_entity/equipment/category/list";
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "redirect:/domain_entity/equipment/category/list";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addEquipmentCategory(EquipmentCategory category,
			@RequestParam(required = false) String[] attrs,
			@RequestParam(required = false) long[] types) {
		logger.info("添加 EquipmentCategory:" + new Date().toString()
				+ "----start");
				equipmentCategoryService.addEquipmentCategory(category);
			 if (types != null && attrs != null && types.length == attrs.length) {
				for (int i = 0; i < types.length; i++) {
					EquipmentProperty property = new EquipmentProperty();
					property.setCategory(category);
					property.setName(attrs[i]);
					DomainEntity type = domainEntityService.getDomainEntityById(types[i]);
					property.setType(type);
					equipmentPropertyService.addEquipmentProperty(property);
				}
			}
		logger.info("添加 EquipmentCategory:" + new Date().toString()
				+ "----end");
		return "redirect:/domain_entity/equipment/category/list";
	}
}
